package com.spread.libserver.controller;

import com.spread.libserver.doer.dofactory.Operation;
import com.spread.libserver.model.constant.AccountType;
import com.spread.libserver.model.network.BookResponse;
import com.spread.libserver.model.network.CategoryResponse;
import com.spread.libserver.model.network.Response;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * All operations that a user can do.
 */
@RestController
public class UserController {

    @GetMapping("userop/register")
    public Response registerUser(@RequestParam("account") String acc,
                                 @RequestParam("password") String pwd){
        return Operation.register(acc, pwd, AccountType.USER);
    }

    @GetMapping("userop/login")
    public Response loginUser(@RequestParam("account") String acc,
                              @RequestParam("password") String pwd){
        return Operation.login(acc, pwd);
    }

    @GetMapping("userop/changepassword")
    public Response changePasswordUser(@RequestParam("account") String acc,
                                       @RequestParam("target") String targetAcc,
                                       @RequestParam("newpswd") String newPassword){
        return Operation.changePassword(acc, targetAcc, newPassword, AccountType.USER);
    }

    @GetMapping("userop/getallcategory")
    public CategoryResponse getAllCategoriesUser(){
        return Operation.getAllCategories();
    }

    @GetMapping("userop/getbook/bycategory")
    public BookResponse getBookByCategoryUser(@RequestParam("category") String category){
        return Operation.getBookByCategory(category);
    }

    @GetMapping("userop/getbook/byname")
    public BookResponse getBookByNameManager(@RequestParam("name") String name){
        return Operation.getBookByName(name);
    }

    @Transactional
    @GetMapping("userop/borrow")
    public Response borrowBook(@RequestParam("bookid") int id,
                               @RequestParam("duration") int duration,
                               @RequestParam("fine") float fine,
                               @RequestParam("account") String account){
        return Operation.borrowBook(id, duration, fine, account);
    }

    @Transactional
    @GetMapping("userop/return")
    public Response returnBook(@RequestParam("bookid")int bookId,
                               @RequestParam("damaged") boolean isDamaged,
                               @RequestParam("account") String account,
                               @RequestParam("borrowid") int borrowId){
        return Operation.returnBook(bookId, isDamaged, account, borrowId);
    }



}
